package com.hspedu.newData.dmsxl.数组.双指针;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC977有序数组的平方 {

    public int[] sortedSquares(int[] nums) {

        int len = nums.length - 1;
        int[] result = new int[len + 1];
        int left = 0;
        int right = len;

        while (left <= right){
            if (nums[left]*nums[left] > nums[right]*nums[right]){
                result[len--] = nums[left]*nums[left];
                left ++;
            }else {
                result[len--] = nums[right]*nums[right];
                right --;
            }
        }
        return result;
    }

    @Test
    public void test() {
        // 测试代码
        int[] nums = new int[]{-4,-1,0,3,10};

        sortedSquares(nums);
        System.out.println("=========");
    }
}
